#!/bin/sh

t_sync=$1

echo "time to start: $t_sync"

echo "cwd : $PWD"

echo "restoring my $HOME variable"
export HOME=~/
if [ -f $HOME/.bashrc ]; then
    source $HOME/.bashrc
fi
echo "home: $HOME"

export MY_WORKDIR=$PWD

echo "setting up ATLAS runtime environment"
export EventLoopDir=$HOME/EventLoop.r17.grid.dev

cd $EventLoopDir
source $EventLoopDir/setup.sh
rm -f run.log
rm -f ioperf.root

now=`date +%s`
while [[ $now -lt $t_sync ]]; do
    sleep 1
    now=`date +%s`
done

echo "event loop start: $now"

#$EventLoopDir/bin/TimeDriver.exe --MUS 1 --EER 1 --inputFileMode 3 --fileList $EventLoopDir/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p834_tid615381_00/NTUP_TOP.615381._000008.root.1 --outputFilename output.singlefile.allscales.root 2>&1 > run.log
$EventLoopDir/bin/TimeDriver.exe --MUS 1 --EER 1 --inputFileMode 3 --fileList $EventLoopDir/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p834_tid615381_00/NTUP_TOP.615381._000046.root.1 --outputFilename output.singlefile.allscales.root 2>&1 > run.log

now=`date +%s`
echo "event loop stop: $now"

echo "moving output files to the workdir"
mv $EventLoopDir/run.log $MY_WORKDIR
mv $EventLoopDir/ioperf.root $MY_WORKDIR

cd $MY_WORKDIR
